import { createRouter, createWebHistory } from 'vue-router'
import HelloWorld from '@/component/WYK/HelloWorld.vue'
import test from '@/component/WYK/pages/test.vue'

const routes = [
  {
    path: '/',
    component: () => import('@/component/WYK/Login.vue')
  },
  {
    path: '/login',
    component: () => import('@/component/WYK/Login.vue')
  },
  {
    path: '/register',
    component: () => import('@/component/WYK/Register.vue')
  },
  {
    path: '/index',
    component: () => import('@/component/Index.vue'),
    children: [
      {
        path: '/index/home',
        component: () => import('@/component/WYK/pages/Home.vue'),
        children:[
          {
            path: '/HelloWorld',
            name: 'HelloWorld',
            component: HelloWorld
          },
          {
              path: '/test',
              name: 'test',
              component: test
          },
        ]
      },
      {
        path: '/index/hui',
        component: () => import('@/component/WYK/Hui.vue')
      },
      {
        path: '/index/fang',
        component: () => import('@/component/DXH/Fang.vue')
      },
      {
        path: '/index/historyhua',
        component: () => import('@/component/DXH/HistoryHua.vue')
      },
      {
        path: '/index/tong',
        component: () => import('@/component/DXH/Tong.vue')
      },
      {
        path: '/index/jilu',
        component: () => import('@/component/ZLQ/JiLu.vue')
      },
      {
        path: '/index/kehu',
        component: () => import('@/component/ZLQ/KeHu.vue')
      },
      {
        path: '/index/work',
        component: () => import('@/component/ZLQ/Work.vue')
      },
      {
        path: '/index/data',
        component: () => import('@/component/LKR/Data.vue')
      },
      {
        path: '/index/set',
        component: () => import('@/component/LKR/Set.vue')
      },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next();
  } else {
    const accessToken=localStorage.getItem('accessToken');
    const refreshToken=localStorage.getItem('refreshToken');
    if (accessToken && refreshToken) {
      next(); 
    }else{
      next('/login');
    }
  }
});

export default router
